﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Web.Model;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class MoveQuestion
    {
       
        [AjaxPro.AjaxMethod]
        public static string GetHtml(string SiteId,string FirstGroupQuestionId,string FirstMonthiId,string Type, string Keyword, int CurrentPageIndex)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                
                string QuestionId = (string)WebEnvironments.Request(SiteId, "QuestionId");
                QuestionCls QuestionCls = new QuestionBll().CreateModel(SiteId, QuestionId);
                if (QuestionCls == null)
                {
                    return "Không tìm thấy câu hỏi này";
                }


                GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId,null);
                string SelectGroupQuestionText = "<select class=cssComboboxContent id=drpSelectGroupQuestion onchange=javascript:DetectMonthiForMove();>";
                for (int iIndex = 0; iIndex < GroupQuestions.Length; iIndex++)
                {
                    string GroupQuestionId = (string)GroupQuestions[iIndex].GroupQuestionId;
                    string Name = (string)GroupQuestions[iIndex].GroupQuestionName;
                    if (QuestionCls.GroupQuestionId.Equals(GroupQuestionId))
                    {
                        SelectGroupQuestionText += "<option selected value=\"" + GroupQuestionId + "\">" + Name + "</option>";
                    }
                    else
                    {
                        SelectGroupQuestionText += "<option value=\"" + GroupQuestionId + "\">" + Name + "</option>";
                    }
                }
                SelectGroupQuestionText += "</select>";

                DataTable dtMonthi = new MonthiBll().ReadingMonthi(SiteId, QuestionCls.GroupQuestionId, "");
                string SelectMonthiText = "<select class=cssComboboxContent id=drpSelectMonthi onchange=javascript:ResetSearch();>";
                for (int iIndex = 0; iIndex < dtMonthi.Rows.Count; iIndex++)
                {
                    string MonthiId = (string)dtMonthi.Rows[iIndex]["MonthiId"];
                    string Name = (string)dtMonthi.Rows[iIndex]["Name"];
                    if (QuestionCls.MonthiCls.MonthiId.Equals(MonthiId))
                    {
                        SelectMonthiText += "<option selected value=\"" + MonthiId + "\">" + Name + "</option>";
                    }
                    else
                    {
                        SelectMonthiText += "<option value=\"" + MonthiId + "\">" + Name + "</option>";
                    }
                }
                SelectMonthiText += "</select>";

                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
      
                string RetHtml =
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:ExecuteClientMoveQuestion('"+QuestionId+"');\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Chuyển</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('examquestion.aspx','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    "   <h3>" + QuestionCls.Title + "</h3>" +
                    "   <div class=CssFormSaveUpdate>\r\n" +
                    "   <table>" +
                    "       <tr>" +
                    "           <td class=Col1>Nhóm câu hỏi:</td>" +
                    "           <td class=Col2>" + SelectGroupQuestionText + "</td>" +
                    "       </tr>" +
                    "       <tr>" +
                    "           <td class=Col1>Loại câu hỏi:</td>" +
                    "           <td class=Col2><div id=divMonthiContent>" + SelectMonthiText + "</div></td>" +
                    "       </tr>" +
                    //"       <tr>" +
                    //"           <td></td>" +
                    //"           <td><input onclick=javascript:ExecuteClientMoveQuestion('" + QuestionId + "'); type=button class=button  value=\"Chuyển\"><input onclick=\"javascript:window.open('ExamQuestion.aspx','_self');\" type=button class=button  value=\"Quay lại\"></td>" +
                    //"       </tr>" +
                    "   </table>" +
                    "</div>\r\n";

                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetMonthi(string SiteId,string FirstGroupQuestionId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                DataTable dtMonthi = new MonthiBll().ReadingMonthi(SiteId, FirstGroupQuestionId, "");
                string SelectMonthiText = "<select class=cssComboboxContent id=drpSelectMonthi onchange=javascript:ResetSearch();>";
                for (int iIndex = 0; iIndex < dtMonthi.Rows.Count; iIndex++)
                {
                    string MonthiId = (string)dtMonthi.Rows[iIndex]["MonthiId"];
                    string Name = (string)dtMonthi.Rows[iIndex]["Name"];
                    SelectMonthiText += "<option value=\"" + MonthiId + "\">" + Name + "</option>";
                }
                SelectMonthiText += "</select>";
                return SelectMonthiText;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string RetHtml =
                    " <script language=javascript> \r\n" +
                    " function ExecuteClientMoveQuestion(QuestionId) \r\n" +
                    " { \r\n" +
                    "      SiteId='" + SiteId + "';\r\n" +
                    "      MonthiId=document.getElementById(\"drpSelectMonthi\").options[document.getElementById(\"drpSelectMonthi\").selectedIndex].value; \r\n" +
                    "      retValue=Portal.Exam.Render.MoveQuestion.ServerSideMoveQuestion(SiteId, MonthiId,QuestionId).value; \r\n" +
                    "      if(retValue.indexOf(\"ERROR\")!=-1) \r\n" +
                    "      { \r\n" +
                    "         alert(retValue); \r\n" +
                    "         return; \r\n" +
                    "      } \r\n" +
                    "      alert(retValue); \r\n" +
                    "      window.open('ExamQuestion.aspx','_self'); \r\n" +
                    " } \r\n" +
                    "</script>\r\n" +
                     "<div id=divMoveQuestionMain>" + GetHtml(SiteId, "", "", "", "", 0) + "</div>";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmMoveMsg(string SiteId)
        {
            return "Bạn đã chắc chắn chuyển chưa?";
        }



        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            
            return "Đang đọc dữ liệu từ máy chủ";
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideMoveQuestion(string SiteId,string MonthiId, string QuestionId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                new QuestionBll().MoveQuestion(null,SiteId, MonthiId, QuestionId);
                return "Chuyển thành công!";
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }
    }
}
